package de.akabahn.ebd.control.auth;

import static de.akabahn.ebd.comm.Config.FSTCTRL_EOR_CHARS;
import static de.akabahn.ebd.comm.Config.FSTCTRL_WELCOME_MSG;
import static de.akabahn.ebd.control.common.Config.AUTH_SERVER_NAME;
import static de.akabahn.ebd.control.common.Config.AUTH_SERVER_PORT;
import android.text.TextUtils;
import de.akabahn.ebd.comm.telnet.TelnetConnectionClient;
import de.akabahn.ebd.control.data.DebugDataContainer;

/**
 * @author Peter Eimann
 */
public class RemoteAuthenticator extends AbstractAuthenticator {

	private static RemoteAuthenticator instance;

	private TelnetConnectionClient telnetConnectionAuthServer;
	private DebugDataContainer ddc;

	private RemoteAuthenticator() {
		telnetConnectionAuthServer = 
			new TelnetConnectionClient(AUTH_SERVER_NAME, AUTH_SERVER_PORT, FSTCTRL_EOR_CHARS, FSTCTRL_WELCOME_MSG);
		ddc = DebugDataContainer.getInstance();
	}

	public static RemoteAuthenticator getInstance() {
		if (instance == null) {
			instance = new RemoteAuthenticator();
		}

		return instance;
	}

	@Override
	public UserAuthObject authenticateUser(String user, String password) {
		String response = null;
		String[] responseSegments = null;
		UserAuthObject uaObject = null;

		if (!TextUtils.isEmpty(user) && !TextUtils.isEmpty(password)) {
			try {
				telnetConnectionAuthServer.connect();
				telnetConnectionAuthServer.write("CMD=AUTH&USER=" + user + "&PWD=" + md5hash(password));
				response = telnetConnectionAuthServer.read();
				ddc.add("RemoteAuthenticator.authenticateUser()", response);
				if (response != null && response.startsWith("OK")) {
					// Format: OK (role|version|apk-path)
					responseSegments = response.substring(response.indexOf('(') + 1, response.indexOf(')')).split("\\|");
					uaObject = new UserAuthObject(
						user,
						md5hash(password),
						AccessEnum.valueOf(responseSegments[0]),
						responseSegments[1],
						responseSegments[2]);
				}
			} finally {
				telnetConnectionAuthServer.disconnect();
			}
		}

		return uaObject;
	}

	@Override
	public boolean changePassword(String user, String oldPassword, String newPassword) {
		String response = null;
		boolean result = false;

		try {
			telnetConnectionAuthServer.connect();
			telnetConnectionAuthServer.write(
					"CMD=NEWPWD&USER=" + user + "&PWD=" + md5hash(oldPassword) + "&NEWPWD=" + md5hash(newPassword));
			response = telnetConnectionAuthServer.read();
			ddc.add("RemoteAuthenticator.changePassword()", response);
			result = response != null && response.startsWith("OK");
		} finally {
			telnetConnectionAuthServer.disconnect();
		}

		return result;
	}

	@Override
	public boolean manageUser(String user, String password, AccessEnum role) {
		String response = null;
		boolean result = false;

		try {
			telnetConnectionAuthServer.connect();
			telnetConnectionAuthServer.write(
					"CMD=MNGUSER&USER=" + user + "&PWD=" + md5hash(password) + "&ROLE=" + role.name());
			response = telnetConnectionAuthServer.read();
			ddc.add("RemoteAuthenticator.manageUser()", response);
			result = response != null && response.startsWith("OK");
		} finally {
			telnetConnectionAuthServer.disconnect();
		}

		return result;
	}
}
